package com.apache.ius.able;

import java.util.concurrent.Callable;

import com.apache.database.constant.SpringContextLoader;
import com.apache.database.datasource.DynamicDataSource;
import com.apache.database.datasource.DynamicDataSourceHolder;
import com.apache.database.db.IDao;
import com.apache.database.model.MethodParam;
import com.apache.ius.IusSqlTools;

/**
 * description: 跨数据源查询
 *
 * @author dyh 创建时间：2018年07月17日
 */
public class SelectDatasourceCall implements Callable<Object> {

	private String sql;
	private String datasource;
	private int pageIndex = 0;
	private int pageSize = 0;

	public SelectDatasourceCall(String sql, String datasource) {
		this.datasource = datasource;
		this.sql = sql;
	}

	public SelectDatasourceCall(String sql, String datasource, int pageIndex, int pageSize) {
		this.datasource = datasource;
		this.sql = sql;
		this.pageIndex = pageIndex;
		this.pageSize = pageSize;
	}

	@Override
	public Object call() throws Exception {
		IDao dao = (IDao) SpringContextLoader.getBean("iusPubDao");
		if (!DynamicDataSource.getInstance().checkDataSource(datasource)) {
			DynamicDataSource.getInstance().addDataSource(datasource);
		}
		DynamicDataSourceHolder.putDataSource(datasource);//指定数据源
		MethodParam params = new MethodParam("ByObjInfo", "", "", IusSqlTools.SPANCENAME);
		params.setParams("dyncSql", sql);
		if (pageSize > 0 && pageIndex > 0) {
			params.setPageIndex(pageIndex);
			params.setPageSize(pageSize);
			return dao.pageSelect(params);
		}
		return dao.select(params);
	}
}
